/*
 * Yguan - Yui Gwt Unified ANimation
 *
 * Copyright (c) 2009, Yves Cuillerdier
 * All rights reserved.
 * 
 * This software is licensed under the new BSD License:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
 * 
 * Neither the name of the author nor the names of its contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * @version 0.1.0
 */
package gyraf.core.client.animation;

import gyraf.wrapper.client.YuiAnimator;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Element;

/**
 * This class animate an Element.
 * <p>
 * The animations concerns integer attributes, colors, or position (Motion or
 * Scrolling)
 */
public class Animation {

	/** The 4 Animations object */
	private final YuiAnimator attributeAnimator;
	private final YuiAnimator colorAnimator;
	private final YuiAnimator motionAnimator;
	private final YuiAnimator scrollAnimator;

	/**
	 * Create a new Animation for an element
	 * 
	 * @param element
	 *            the element to animate
	 * @param data
	 *            the animation configuration
	 */
	public Animation(final Element element, final AnimationData data) {
		// Create the 4 animations if needed
		attributeAnimator = data.needAttributeAnimation() ? YuiAnimator
				.makeAttributeAnimation(element,
						data.getJsAttributeAnimation(), data.getDuration(),
						data.getJsAnimationEffect()) : null;

		colorAnimator = data.needColorAnimation() ? YuiAnimator
				.makeColorAnimation(element, data.getJsColorAnimation(), data
						.getDuration(), data.getJsAnimationEffect()) : null;
		motionAnimator = data.needMotionAnimation() ? YuiAnimator
				.makeMotionAnimation(element, data.getJsMotionAnimation(), data
						.getDuration(), data.getJsAnimationEffect()) : null;
		scrollAnimator = data.needScrollAnimation() ? YuiAnimator
				.makeScrollAnimation(element, data.getJsScrollAnimation(), data
						.getDuration(), data.getJsAnimationEffect()) : null;
	}

	/**
	 * Starts the animation.
	 */
	public void animate() {
		if (attributeAnimator != null) {
			attributeAnimator.animate();
		}
		if (colorAnimator != null) {
			colorAnimator.animate();
		}
		if (motionAnimator != null) {
			motionAnimator.animate();
		}
		if (scrollAnimator != null) {
			scrollAnimator.animate();
		}
	}

	/**
	 * Checks whether the element is currently animated.
	 * 
	 * @return current value of isAnimated.
	 */
	public boolean isAnimated() {
		if (attributeAnimator != null && attributeAnimator.isAnimated()) {
			return true;
		}
		if (colorAnimator != null && colorAnimator.isAnimated()) {
			return true;
		}
		if (motionAnimator != null && motionAnimator.isAnimated()) {
			return true;
		}
		if (scrollAnimator != null && scrollAnimator.isAnimated()) {
			return true;
		}
		return false;
	}

	/**
	 * Stops the animation. Normally called by AnimMgr when animation completes.
	 * 
	 * @param finish
	 *            If true, animation will jump to final frame.
	 */
	public void stop(final boolean finish) {
		if (attributeAnimator != null) {
			attributeAnimator.stop(finish);
		}
		if (colorAnimator != null) {
			colorAnimator.stop(finish);
		}
		if (motionAnimator != null) {
			motionAnimator.stop(finish);
		}
		if (scrollAnimator != null) {
			scrollAnimator.stop(finish);
		}
	}

	/**
	 * Stops the animation. Normally called by AnimMgr when animation completes.
	 * 
	 * @see Animation#stop(boolean)
	 */
	public void stop() {
		if (attributeAnimator != null) {
			attributeAnimator.stop();
		}
		if (colorAnimator != null) {
			colorAnimator.stop();
		}
		if (motionAnimator != null) {
			motionAnimator.stop();
		}
		if (scrollAnimator != null) {
			scrollAnimator.stop();
		}
	}

	/**
	 * Get the first non null animation object.
	 * 
	 * @return the first non null animation object.
	 */
	final JavaScriptObject getFirstJsObject() {
		if (attributeAnimator != null) {
			return attributeAnimator;
		}
		if (colorAnimator != null) {
			return colorAnimator;
		}
		if (motionAnimator != null) {
			return motionAnimator;
		}
		if (scrollAnimator != null) {
			return scrollAnimator;
		}
		return null;
	}

	/**
	 * Get the last non null animation object.
	 * 
	 * @return the last non null animation object.
	 */
	final JavaScriptObject getLastJsObject() {
		if (scrollAnimator != null) {
			return scrollAnimator;
		}
		if (motionAnimator != null) {
			return motionAnimator;
		}
		if (colorAnimator != null) {
			return colorAnimator;
		}
		if (attributeAnimator != null) {
			return attributeAnimator;
		}
		return null;
	}
}
